package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.setMealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private setMealMapper setMealMapper;

    @Autowired
    private SetMealDishMapper setMealDishMapper;
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> select = setMealMapper.select(setmealPageQueryDTO);

        return new PageResult(select.getTotal(),select.getResult());
    }

    @Override
    @Transactional
    public void add(SetmealDTO setmealDTO) {
        //新增套餐表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setStatus(StatusConstant.ENABLE);
        setMealMapper.insert(setmeal);
        //新增菜品套餐表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(dish->dish.setSetmealId(setmeal.getId()));
        setMealDishMapper.insert(setmealDishes);
        //将添加的新套餐添加进入分类表中


    }

    @Override
    public SetmealVO select(Integer id) {
        SetmealVO setmealVO;
        setmealVO=setMealMapper.selectById(id);
        List<SetmealDish> setmealDishes = setMealDishMapper.selectBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Override
    @Transactional
    public void deleteByIds(String ids) {
        List<Integer> collect = Arrays.stream(ids.split(",")).map(Integer::parseInt).collect(Collectors.toList());
        //删除套餐
        setMealMapper.delete(collect);
        //删除套餐菜品
        setMealDishMapper.delete(collect);

    }

    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        //修改套餐
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setMealMapper.update(setmeal);
        //修改套餐菜品
        //先删除改套餐id关联的菜品
        List<Integer> list = new ArrayList<>();
        list.add(Math.toIntExact(setmeal.getId()));
        setMealDishMapper.delete(list);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(dish->dish.setSetmealId(setmeal.getId()));
        setMealDishMapper.insert(setmealDishes);
    }

    public void status(Integer status, Integer id) {
        setMealMapper.set(status,id);
    }
}
